//
//  GDBusStationNumberController.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/13.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDBusStationNumberController.h"
#import "MBProgressHUD+MJ.h"
#import "GDBusStationNumber.h"
#import "GDTansitionModelDateOperation.h"
#import "GDBusLineViewController.h"
@interface GDBusStationNumberController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSArray * arrayModel;
@end

@implementation GDBusStationNumberController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIBarButtonItem * backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回上一级";
    self.navigationItem.backBarButtonItem = backItem;
    
    self.view.backgroundColor = [UIColor whiteColor];
    [MBProgressHUD showMessage:@"正在查询,请稍后..." toView:self.view];
    /**
     *  触发查询按钮后，获取到公交路线
     *
     */
    [GDTansitionModelDateOperation initDatewithBusStationNumber:self success:^(NSArray *model) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showSuccess:@"查询成功" toView:self.view];
        self.arrayModel = model;
        [self updateUI:model];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error] toView:self.view];
        [self.navigationController popViewControllerAnimated:YES];
    }];

}

#pragma mark 更新UI
-(void)updateUI:(NSArray *) stationNumberModelArray{
//    GDBusStationNumber * model = stationNumberModelArray[0];
//    GDLog(@"%@",model.Name);
    
    // 共搜索到2条路线
    CGFloat labelX = GDMargin * 2;
    CGFloat labelY = GDMargin  ;
    CGFloat labelW = 200;
    CGFloat labelH = 30;
    UILabel * lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelW, labelH)];
    lineLabel.text = [NSString stringWithFormat:@"共搜索到%ld条路线",(unsigned long)self.arrayModel.count];
    lineLabel.textColor = [UIColor blackColor];
    [self.view addSubview:lineLabel];
//    // 显示数据
    CGFloat dataViewX = GDMargin;
    CGFloat dataViewY = CGRectGetMaxY(lineLabel.frame) + 30;
    CGFloat dataViewW = GDScreenSize.size.width - GDMargin * 2;
    CGFloat dataViewH =  self.arrayModel.count * 50;
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(dataViewX, dataViewY, dataViewW, dataViewH) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayModel.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * ID = @"cell";
    UITableViewCell * cell  = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.backgroundColor = [UIColor colorWithRed:53 / 255.0 green:171 / 255.0 blue:51 / 255.0 alpha:1];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    GDBusStationNumber * model = self.arrayModel[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ [%@ -- %@]",model.LineNumber,model.FromStation,model.ToStation];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",model.StationCount];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    cell.separatorInset = UIEdgeInsetsZero;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GDBusStationNumber * model = self.arrayModel[indexPath.row];
    GDBusLineViewController * lineVieController =[[GDBusLineViewController alloc] init];
    lineVieController.busModel = model;
    [self.navigationController pushViewController:lineVieController animated:YES];
}

@end
